package foodmaterial.action.valid;

import foodmaterial.utils.Constants;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class PaginationSizeValidator implements ConstraintValidator<PaginationSizeValid, Integer> {

    private PaginationSizeValid constraintAnnotation ;

    @Override
    public void initialize(PaginationSizeValid constraintAnnotation) {
        this.constraintAnnotation = constraintAnnotation ;
    }

    @Override
    public boolean isValid(Integer pageSize, ConstraintValidatorContext constraintValidatorContext) {
        if (pageSize == null && constraintAnnotation.ignoreEmpty()) return true ;
        if (pageSize == null) return false ;
        if (pageSize.intValue() > Constants.PAGE_SIZE.intValue() || pageSize.intValue() <= 0) return false ;
        return true;
    }
}
